/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ThreadClient;

import InterfaceClient.JDriveCombo;
import Adaptor.JLocationListValue;
import Packets.pkValue;
import StaticClass.ArrSocket;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;
//import sun.java2d.pipe.ValidatePipe;
//import sun.security.jca.JCAUtil;

/**
 *
 * @author Oho
 */
public class ThreadFile_Tree extends Thread {

    Socket sk = null;
    int port;

    public ThreadFile_Tree(int port) {
        this.port = port;
    }

    public ThreadFile_Tree(Socket sk) {
        this.sk = sk;
    }
    ObjectInputStream inp = null;
    ObjectOutputStream outp = null;
    ComboBoxModel md = null;
    ArrayList<String> arrLabels = new ArrayList<String>();
    ArrayList<JLabel> arrLabels1 = new ArrayList<JLabel>();

    public void getListIcon(File f) {
//        File f = new File(f);


        File[] fl = f.listFiles();
        for (int i = 0; i < fl.length; i++) {
            FileSystemView view = FileSystemView.getFileSystemView();
            Icon theIcon = view.getSystemIcon(f);
            JLabel lb = new JLabel(theIcon);
            arrLabels1.add(lb);
        }
    }
    String parent;

    public JLabel getIcon(File fi) {
        JLabel renderer = null;
        //  try {
        String theText = (fi != null) ? fi.getName().toString() : "";
        //parent = fi.getCanonicalPath();
        //parent = fi.getParent();

        parent = (!parent.endsWith(System.getProperty("file.separator"))) ? parent + System.getProperty("file.separator") : parent;
        String str = parent + theText;
        File f = new File(str);

        FileSystemView view = FileSystemView.getFileSystemView();
        Icon theIcon = view.getSystemIcon(f);
        ArrSocket.icon.add(theIcon);
        renderer = new JLabel(theIcon);

        if (theIcon != null) {
            renderer.setIcon(theIcon);
        }
        renderer.setText(theText);

        // } 
        return renderer;
    }

    public void SendImages(JLocationListValue jlist) {
        try {
            for (int i = 0; i < jlist.getModel().getSize(); i++) {
                String lb = jlist.getModel().getElementAt(i).toString();
                JLabel lb1 = getIcon(new File(lb));
                arrLabels1.add(lb1);
                // System.out.println(lb.getText());

            }
            outp.writeObject(arrLabels1);
            outp.flush();
            outp.reset();
            arrLabels1.clear();
        } catch (IOException ex) {
            Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void ThreadRead() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {


        if (inp == null) {
            inp = new ObjectInputStream(sk.getInputStream());

        }
        // if(outp==null)

        pkValue name = (pkValue) inp.readObject();
        //  JDriveCombo2 cbb = new JDriveCombo2();
        if (md == null) {
            md = new DefaultComboBoxModel(File.listRoots());
            outp.writeObject(md);
            outp.flush();
        }
        parent = name.getObj().toString();
        //outp.writeObject(arrLabels);
        outp.flush();
        JLocationListValue jList1Files = new JLocationListValue(JLocationListValue.FILETYPE_FILE, false, null);
        JLocationListValue jList1Folder = new JLocationListValue(JLocationListValue.FILETYPE_FOLDER, false, jList1Files, null);
        jList1Files.setParentFile(name.getObj().toString());
        jList1Folder.setParentFile(name.getObj().toString());
        //  for (int i=0; i<size; i++) {
        SendImages(jList1Files);
        ArrSocket.icon.clear();
        SendImages(jList1Folder);

        //item = jList1Files.getModel().getElementAt(0);


        //  Icon ic =  ArrSocket.icon.get(0);
//        for(int i =0;i<jList1Folder.getMaxSelectionIndex();i++)
        {
            // selFromList = jList1Folder.getComponent(0);
            //System.out.println("");
        }
        //  BufferedOutputStream bos = new BufferedOutputStream(sk.getOutputStream());
//DataOutputStream dos = new DataOutputStream(bos);

        outp.writeObject(jList1Files);
        outp.writeObject(jList1Folder);
        outp.flush();
    }

    @Override
    public void run() {
        try {
            outp = new ObjectOutputStream(sk.getOutputStream());
            while (true) {
                try {
                    try {
                        ThreadRead();
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
